<?php


namespace app\controller\equipment;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 补货管理
 */
class TakeCargo extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取终端缺货列表
     */
	public function api_equipment_takeCargo_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'equipment_code' => isset($msg['data']['equipment_code'])?$msg['data']['equipment_code']:'',
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'goods_name' => isset($msg['data']['goods_name'])?$msg['data']['goods_name']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0 AND threshold >= surplus_number AND pathway_status = 2";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['equipment_code'])){
            $where .= " AND equipment_code LIKE '%".$info['equipment_code']."%'";
        }
        if(!empty($info['goods_name'])){
            $where .= " AND goods_name LIKE '%".$info['goods_name']."%'";
        }
		$list = $db->query("select * from equipment_goods where ".$where ." GROUP BY equipment_code,goods_id order by id  desc limit ".$limit); 
        $count =  $db->query("select * from equipment_goods where ".$where ." GROUP BY equipment_code,goods_id order by id  desc");
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $goods = $this->table->find('goods',['id'=>$v['goods_id']]);
                $list[$k]['category_name'] = $goods['category_name_2'];
                $list[$k]['units_name'] = $goods['units_name'];
                $list[$k]['brand_name'] = $goods['brand_name'];
            }
        }
        return $this->outMessage(10000,'获取成功',$list,'success',count($count));
	}



    /**
     * 生成提货申请单
     */
    public function api_equipment_takeCargo_add($msg)
    {
        $info = [
            'merchant_id' =>  isset($msg['data']['merchant_id'])?$msg['data']['merchant_id']:'',
            'take_depot_id' => isset($msg['data']['take_depot_id'])?$msg['data']['take_depot_id']:'',
        ];
        $list = isset($msg['data']['list'])?$msg['data']['list']:'';
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        if(!is_array($list) || empty($list)){
            return $this->outMessage(-1,'提货商品信息不能为空');
        }
        $info['remark'] = isset($msg['data']['remark'])?$msg['data']['remark']:'';
        if(!empty($info['remark'])){
            if(!$this->validate->checkName($info['remark'],2,100)){
                return $this->outMessage(-1,'请输入正确的备注');
            }
        } 
        $merchant_info = $this->table->find('equipment_list',['merchant_id'=>$info['merchant_id']]);
        $depot = $this->table->find('depot',['id'=>$info['take_depot_id']]);
        $number = 0;
        $money = 0;
        foreach ($list as $v) {
            $goods = $this->table->find('goods',['id'=>$v['goods_id']]);
            $number += $v['number'];
            $money += $goods['buy_price']*$v['number'];
        }
        $data = [
            'order_no' => $this->common->createOutTradeNo('T'),
            'merchant_id' => $merchant_info['merchant_id'],
            'merchant_name' => $merchant_info['merchant_name'],
            'itoc_id' => $merchant_info['itoc_id'],
            'itoc_name' => $merchant_info['itoc_name'],
            'take_itoc_id' => $depot['itoc_id'],
            'take_itoc_name' => $depot['itoc_name'],
            'take_depot_id' => $info['take_depot_id'],
            'take_depot_name' => $depot['name'],
            'take_depot_address' => $depot['region_name'].$depot['address'],
            'number' => $number,
            'money' => $money,
            'remark' => $info['remark'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('take_cargo',$data);
        if(!$res){
            return $this->outMessage(-1,'提交失败');
        }
        foreach ($list as $v) {
            $goods = $this->table->find('goods',['id'=>$v['goods_id']]);
            $goods_data['order_no'] = $data['order_no'];
            $goods_data['goods_id'] = $v['goods_id'];
            $goods_data['goods_code'] = $goods['code'];
            $goods_data['goods_name'] = $goods['name'];
            $goods_data['category_id'] = $goods['category_id_2'];
            $goods_data['category_name'] = $goods['category_name_2'];
            $goods_data['brand_id'] = $goods['brand_id'];
            $goods_data['brand_name'] = $goods['brand_name'];
            $goods_data['attribute'] = $goods['attribute'];
            $goods_data['units_name'] = $goods['units_name'];
            $goods_data['number'] = $v['number'];
            $goods_data['price'] = $goods['buy_price'];
            $goods_data['sum_price'] = $goods['buy_price']*$v['number'];
            $goods_data['create_time'] = date('Y-m-d H:i:s',time());
            $res = $this->table->insert('take_cargo_goods',$goods_data);
            if(!$res){
                return $this->outMessage(-1,'提交失败');
            }
        }
        return $this->outMessage(10000,'提交成功');
    }




    /**
     * 获取提货申请列表
     */
    public function api_equipment_takeCargo_takeCargoList($msg)
    {   
        global $db;
        $info = [
            'page' => isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'order_no' => isset($msg['data']['order_no'])?$msg['data']['order_no']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'merchant_name' => isset($msg['data']['merchant_name'])?$msg['data']['merchant_name']:'',
            'take_depot_name' => isset($msg['data']['take_depot_name'])?$msg['data']['take_depot_name']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['status'])){
            $where .= " AND status =".$info['status'];
        }
        if(!empty($info['order_no'])){
            $where .= " AND order_no LIKE '%".$info['order_no']."%'";
        }
        if(!empty($info['itoc_name'])){
            $where .= " AND itoc_name LIKE '%".$info['itoc_name']."%'";
        }
        if(!empty($info['merchant_name'])){
            $where .= " AND merchant_name LIKE '%".$info['merchant_name']."%'";
        }
        if(!empty($info['take_depot_name'])){
            $where .= " AND take_depot_name LIKE '%".$info['take_depot_name']."%'";
        }
        $list = $db->query("select * from take_cargo where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from take_cargo WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
    }


    /**
     * 获取提货详情
     */
    public function api_equipment_takeCargo_detailsList($msg)
    {   
        global $db;
        $order_no = isset($msg['data']['order_no'])?$msg['data']['order_no']:'';
        if(empty($order_no)){
            return $this->outMessage(-1,'提货申请编号不能为空');
        }
        $list = $this->table->select('take_cargo_goods',['order_no'=>$order_no]);
        return $this->outMessage(10000,'获取成功',$list);
    }



    /**
     * 终端补货统计
     */
    public function api_equipment_takeCargo_supplyStat($msg)
    {   
        global $db;
        $info = [
            'page' => isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'date' => isset($msg['data']['date'])?$msg['data']['date']:1,
            'take_depot_name' => isset($msg['data']['take_depot_name'])?$msg['data']['take_depot_name']:'',
            'goods_name' => isset($msg['data']['goods_name'])?$msg['data']['goods_name']:'',
        ];
        if(empty($info['date']) || !is_numeric($info['date']) || $info['date'] < 1 || $info['date'] > 4){
            return $this->outMessage(-1,'时间筛选错误');
        }
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = " a.status = 3";
        if(!empty($info['goods_name'])){
            $where .= " AND b.goods_name LIKE '%".$info['goods_name']."%'";
        }
        if(!empty($info['take_depot_name'])){
            $where .= " AND a.take_depot_name LIKE '%".$info['take_depot_name']."%'";
        }
        $group = '%Y-%m';
        if($info['date'] == 4){
            $group = '%Y';
        }
        if($info['date'] == 1 || $info['date'] ==4){
            $list = $db->query("SELECT date_format(b.create_time,'".$group."') time,a.take_depot_name,b.goods_code,b.goods_name,b.brand_name,SUM(reality_number) as number FROM take_cargo as a left join take_cargo_goods as b on a.order_no = b.order_no WHERE ".$where." GROUP BY b.goods_id,a.take_depot_id,date_format(b.create_time,'".$group."') order by b.create_time limit ".$limit);

            $count = $db->query("SELECT date_format(b.create_time,'".$group."') time,a.take_depot_name,b.goods_code,b.goods_name,b.brand_name,SUM(reality_number) as number FROM take_cargo as a left join take_cargo_goods as b on a.order_no = b.order_no WHERE ".$where." GROUP BY b.goods_id,a.take_depot_id,date_format(b.create_time,'".$group."') ");
        }
        if($info['date'] == 3){
            $list = $db->query("SELECT concat(date_format(b.create_time, '%Y'),FLOOR((date_format(b.create_time, '%m')+2)/3)) as time,a.take_depot_name,b.goods_code,b.goods_name,b.brand_name,SUM(reality_number) as number FROM take_cargo as a left join take_cargo_goods as b on a.order_no = b.order_no WHERE ".$where." GROUP BY b.goods_id,a.take_depot_id,concat(date_format(b.create_time, '%Y'),FLOOR((date_format(b.create_time, '%m')+2)/3)) order by b.create_time limit ".$limit);
            $count = $db->query("SELECT concat(date_format(b.create_time, '%Y'),FLOOR((date_format(b.create_time, '%m')+2)/3)) as time,a.take_depot_name,b.goods_code,b.goods_name,b.brand_name,SUM(reality_number) as number FROM take_cargo as a left join take_cargo_goods as b on a.order_no = b.order_no WHERE ".$where." GROUP BY b.goods_id,a.take_depot_id,concat(date_format(b.create_time, '%Y'),FLOOR((date_format(b.create_time, '%m')+2)/3))");
        }
        return $this->outMessage(10000,'获取成功',$list,'success',count($count));  
    }

}
